import logging
import re
from xml.etree import ElementTree
from wcferry import WxMsg, Wcf
from modules.module_base import ModuleBase


class NewFriendsModule(ModuleBase):
    def __init__(self):
        self.log = logging.getLogger(__name__)

    def run(self, msg: WxMsg, wcf: Wcf):
        if msg.type == 37:  # 好友请求
            try:
                xml = ElementTree.fromstring(msg.content)
                v3 = xml.attrib["encryptusername"]
                v4 = xml.attrib["ticket"]
                scene = int(xml.attrib["scene"])
                wcf.accept_new_friend(v3, v4, scene)
            except Exception as e:
                self.log.error(f"同意好友出错：{e}")
        elif msg.type == 10000:  # 系统信息
            nickName = re.findall(r"你已添加了(.*)，现在可以开始聊天了。", msg.content)
            if nickName:
                wcf.send_text(f"Hi {nickName[0]}，这是我们的粉丝交流群, 欢迎加入!", msg.sender)
                wcf.invite_chatroom_members("43082420810@chatroom", msg.sender)

